package com.huangpeng.sdst.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huangpeng.sdst.entity.Order;
import com.huangpeng.sdst.entity.OrderQueryBo;
import com.huangpeng.sdst.entity.SearchBo;
import com.huangpeng.sdst.mapper.OrderMapper;
import com.huangpeng.sdst.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author huangpeng
 * @Title 订单服务实现
 * @Package
 * @Date 2020/8/25 16:05
 * @Description
 * @Version 1.0
 */
@Slf4j
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public boolean saveOrder(Order order) {
        order.setCreateTime(new Date());
        order.setIsDeleted((byte)0);
        return super.save(order);
    }

    @Override
    public IPage<Order> getOrderPageList(SearchBo<OrderQueryBo> queryBo) {
        IPage<Order> page = null;
        try{
            // 分页信息
            Page<Order> pageInfo = new Page<>(queryBo.getPageIndex(), queryBo.getPageSize());
            //业务查询条件
            OrderQueryBo searchBo = queryBo.getSearchParams();
            // 查询过滤
            QueryWrapper<Order> queryWrapper = new QueryWrapper<Order>();
            //静态条件
            queryWrapper.eq("is_deleted",0);
            queryWrapper.orderByDesc("create_time");
            //动态条件
            if(StringUtils.isNotBlank(searchBo.getGoodsName())){
                queryWrapper.like("goods_name",searchBo.getGoodsName());
            }
            if(Objects.nonNull(searchBo.getStartAmountMoney())){
                queryWrapper.gt("amount_money",searchBo.getStartAmountMoney());
            }
            if(Objects.nonNull(searchBo.getEndAmountMoney())){
                queryWrapper.lt("amount_money",searchBo.getEndAmountMoney());
            }
            //执行查询
            page = orderMapper.selectPage(pageInfo, queryWrapper);
        }catch(Exception e){
            log.error("系统异常");
            throw e;
        }

        return page;
    }

    @Override
    public Integer deleteOrder(Long orderId) {
        return orderMapper.deleteById(orderId);
    }

    @Override
    public boolean updateOrder(Order order) {
        order.setCreateTime(new Date());
        order.setIsDeleted((byte)0);
        return super.updateById(order);
    }

    @Override
    public List<Order> getOrderList(SearchBo<OrderQueryBo> queryBo) {
        List<Order> boList = null;
        try{
            //业务查询条件
            OrderQueryBo searchBo = queryBo.getSearchParams();
            // 查询过滤
            QueryWrapper<Order> queryWrapper = new QueryWrapper<Order>();
            //静态条件
            queryWrapper.eq("is_deleted",0);
            queryWrapper.orderByDesc("create_time");
            //动态条件
            if(StringUtils.isNotBlank(searchBo.getGoodsName())){
                queryWrapper.like("goods_name",searchBo.getGoodsName());
            }
            if(Objects.nonNull(searchBo.getStartAmountMoney())){
                queryWrapper.gt("amount_money",searchBo.getStartAmountMoney());
            }
            if(Objects.nonNull(searchBo.getEndAmountMoney())){
                queryWrapper.lt("amount_money",searchBo.getEndAmountMoney());
            }
            //执行查询
            boList = orderMapper.selectList(queryWrapper);
        }catch(Exception e){
            log.error("系统异常");
            throw e;
        }

        return boList;
    }

    @Override
    public Order getOrderById(Long orderId) {
        return orderMapper.selectById(orderId);
    }
}
